<?php
namespace app\admin\controller;

\think\Loader::import('controller/Controller', \think\Config::get('traits_path') , EXT);

use app\admin\Controller;
use think\Db;
use think\Config;

class Leave extends Controller
{
    use \app\admin\traits\controller\Controller;
    // 方法黑名单
    protected static $blacklist = [];

    /**
     * 首页
     * @return mixed
     */
    public function index()
    {
        $data = input();
        $map = array();

        if ($data['realname']) {
            $map['u.realname'] = array('like',"%{$data['realname']}%");
        }

        if ($data['account']) {
            $map['u.account'] = array('like',"%{$data['account']}%");
        }

        if ($data['start_time']) {
            $map['l.start_time'] = $data['start_time'];
        }
        if ($data['end_time']) {
            $map['l.end_time'] = $data['end_time'];
        }
        if ($data['leave_status']) {
            $map['l.leave_status'] = $data['leave_status'];
        }

        if ($this::$isdelete){
            $map['l.isdelete'] = 1;
        }else{
            $map['l.isdelete'] = 0;
        }

        $listRows = $this->request->param('numPerPage') ?20: Config::get("paginate.list_rows");
        $list = Db::name('leave')
            ->alias('l')
            ->join('admin_user u', 'u.id = l.user_id', 'LEFT')
            ->field('l.*,u.realname,u.account')
            ->where($map)
            ->order("l.id desc")
            ->paginate($listRows, false, ['query' => $this->request->get()]);

        // 模板赋值显示
        $this->view->assign('list', $list);
        $this->view->assign("count", $list->total());
        $this->view->assign("page", $list->render());
        $this->view->assign('numPerPage', $list->listRows());
        return $this->view->fetch();
    }

    /**
     * 同意
     * @return mixed
     */
    public function agree(){
      //获取传递过来的数据
      $data = input();
      // 验证当前司机的请假时间，是否与进行中与派单中的派单的出发时间与结束时间冲突
        $userLeave = Db::name('leave')->where('id',$data['id'])->find();
        if (!empty($userLeave['start_time']) && !empty($userLeave['end_time'])){
            $tian = round((strtotime($userLeave['end_time'])-strtotime($userLeave['start_time']))/3600/24);
            $check_in_date = $userLeave['start_time'];
            // 把开始时间与结束时间的相差天数
            $date = [$check_in_date];
            logs('cha',$date);
            for ($i=1; $i<= $tian; $i++){
                array_push($date,date("Y-m-d",strtotime("+{$i} day",strtotime($userLeave['start_time']))));//添加的
            }
        }
        if($date){
            foreach ($date as $dateItem){
                $dispatchOrder = Db::name('dispatch_traveler_group')
                    ->where('driver_user_id',$userLeave['user_id'])
                    ->where('dispatch_group_status','in','sendorders,proceed')
                    ->where('go_date','<=',strtotime($dateItem))
                    ->where('end_date','>=',strtotime($dateItem))
                    ->select();
                if ($dispatchOrder){
                    foreach ($dispatchOrder as $index => $item){
                        $start = empty($userLeave['start_time'])?'':$userLeave['start_time'];
                        $end = empty($userLeave['end_time'])?'':$userLeave['end_time'];
                        return ajax_return_adv_error("司机请假时间:{$start}至{$end},与司机的派单id为:{$item['id']}的订单时间冲突!");
                    }

                }
            }
        }
      $data['isagreee']=1;
      $data['leave_status']='agree';
      $data['handle_time']=NOW_TIME;
      $data['handle_user']=Db::name('admin_user_info')->where('id',UID)->value('realname');
      $update = Db::name("leave")->where('id', $data['id'])->update($data);
      //发送消息
        if ($update){
            //根据请假条查询用户信息
            $userLeave = Db::name('leave')->where('id',$data['id'])->find();
            set_info($userLeave['user_id'],'请假结果',"你好,你申请的请假时间{$userLeave['start_time']}到{$userLeave['end_time']},申请结果为通过",'system_ordinary_info');
            return ajax_return_adv_error('修改成功！');
        }
    }

    /**
     * 不同意
     * @return mixed
     */
    public function disagree(){
        //获取传递过来的数据
        $data = input();

        $data['isagreee']=0;
        $data['leave_status']='refuse';
        $data['handle_time']=NOW_TIME;
        $data['handle_user']=Db::name('admin_user')->where('id',UID)->value('realname');

        $update = Db::name("leave")->where('id', $data['id'])->update($data);
        //发送消息
        if ($update){
            //根据请假条查询用户信息
            $userLeave = Db::name('leave')->where('id',$data['id'])->find();
            set_info($userLeave['user_id'],'请假结果',"你好,你申请的请假时间{$userLeave['start_time']}-{$userLeave['end_time']},申请结果为不通过",'system_ordinary_info');
        }
    }

    /**
     * 添加
     * @return mixed
     */
    public function add()
    {
        $data=input();
        //如果是ajax传参
        if($this->request->isAjax()){
                $id=Db::name('leave')->insertGetId($data);
                if($id){
                    return ajax_return_adv("添加成功");
                }
        }else{
            //添加页面展示

            //获取用户信息
            $users=Db::name('admin_user')->select();
            $this->view->assign('users',$users);
            return $this->view->fetch('edit');
        }

    }

    /**
     * 渲染编辑页面
     * @return mixed
     */
    public function beforeedit()
    {
        if(!$this->request->isAjax()){

            //获取用户信息
            $users=Db::name('admin_user')->select();
            $this->view->assign('users',$users);
            return $this->view->fetch('edit');
        }
    }

    /**司机详细资料**/
    public function details()
    {
        $data = input();

        $vo = Db::name('driver')->where('user_id', $data['user_id'])->find();
        if (!$vo){
            return ajax_return_adv_error('不存在该司机信息');
        }
        $users = Db::name('admin_user')->where('id',$vo['user_id'])->find();
        if ($users['id']){
            $users['user_id'] = $users['id'];
            unset($users['id']);
        }
        if (!$users){
            return ajax_return_adv_error('不存在该司机的用户信息',1);
        }
        //查询司机等级
        $driverRank = Db::name('driver_rank')->where('id',$vo['driver_rank_id'])->find();
        if ($driverRank){
            $vo['driver_rank_id'] = $driverRank['name'];
        }else{
            $vo['driver_rank_id'] = '新手';
        }
        $this->view->assign('users', $users);
        $this->view->assign('vo', $vo);
        return $this->view->fetch('details');
    }
}
